package jk.spider.util.seed.impl;

import java.util.List;
import java.util.Map;

import jk.spider.core.SpiderController;
import jk.spider.model.SeedInfo;
import jk.spider.util.config.ConfigConstants;
import jk.spider.util.config.PropertySet;
import jk.spider.util.db.DBHelp;
import jk.spider.util.seed.Seed;

import org.apache.log4j.Logger;

public class SqlSeed extends Seed {
	protected static final Logger log = Logger.getLogger(SqlSeed.class);
	protected static final String SEEDTYPE = "seed";
	protected DBHelp dbHelp = DBHelp.getInstant();
	
	public SqlSeed(SpiderController controller, PropertySet props) {
		super(controller, props);
	}

	public void dealSeed() {
		int count = 0;
		String sql = props.getString(ConfigConstants.SEED_FILE, "");
		List<Map<String, Object>> seedInfo = dbHelp.query(sql);
		for(int i = 0; i < seedInfo.size(); i++) {
			count ++;	
			SeedInfo sdInfo =  new SeedInfo();
			sdInfo.setKid(seedInfo.get(i).get("kid").toString());
			sdInfo.setUrl(seedInfo.get(i).get("url").toString());
			sdInfo.setSite(seedInfo.get(i).get("site").toString());
			sdInfo.setSeedType(SEEDTYPE);
			controller.getStorage().getResourceDAO().createSeed(sdInfo);
		}
		log.info("Seed Count " + count);
	}
}